package com.soft.mainsite.common.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Maps;
import com.soft.mainsite.common.domain.ErrorCode;
import com.soft.mainsite.module.api.vo.ApiResult;

import java.util.List;
import java.util.Map;

/**
 * Description: JsonUtil
 *
 * @author Jin
 * @create 2017-07-27
 */
public class JsonUtil {

    private final static Integer DEFAULT_ERROR_CODE = 1;

    private final static Integer DEFAULT_SUCCESS_CODE = 0;

    /**
     * 后台框架请求成功JSON
     *
     * @param message
     * @param tabId
     * @param closeCurrent
     * @return
     */
    public static String toSuccess(String message, String tabId, boolean closeCurrent) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", 200);
        obj.put("message", message);
        obj.put("tabid", tabId);
        obj.put("closeCurrent", closeCurrent);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架请求成功JSON
     *
     * @param code
     * @return
     */
    public static String toSuccess(int code) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", 200);
        obj.put("message", ErrorCode.getDesc(code));
        obj.put("tabid", "");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }


    /**
     * 后台框架请求成功JSON
     *
     * @param message
     * @param tabId
     * @param dialogid
     * @param closeCurrent
     * @return
     */
    public static String toSuccess(String message, String tabId, String dialogid, boolean closeCurrent) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", message);
        obj.put("tabid", tabId);
        obj.put("dialogid", dialogid);
        obj.put("closeCurrent", closeCurrent);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架请求成功JSON
     *
     * @param message
     * @param tabId
     * @param dialogid
     * @param divid
     * @param closeCurrent
     * @return
     */
    public static String toSuccess(String message, String tabId, String dialogid, String divid, boolean closeCurrent) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", message);
        obj.put("tabid", tabId);
        obj.put("dialogid", dialogid);
        obj.put("closeCurrent", closeCurrent);
        obj.put("divid", divid);
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架请求成功JSON
     *
     * @param message
     * @param tabId
     * @param dialogid
     * @param divid
     * @param forward
     * @param closeCurrent
     * @return
     */
    public static String toSuccess(String message, String tabId, String dialogid, String divid, String forward, boolean closeCurrent) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", message);
        obj.put("tabid", tabId);
        obj.put("dialogid", dialogid);
        obj.put("closeCurrent", closeCurrent);
        obj.put("divid", divid);
        obj.put("forward", forward);
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    public static String toSuccess() {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", "操作成功");
        obj.put("tabid", "");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架请求成功JSON
     *
     * @param message
     * @return
     */
    public static String toSuccess(String message) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", message);
        obj.put("tabid", "");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架文件上传成功JSON
     *
     * @param message
     * @param fileName
     * @return
     */
    public static String toUploadSuccess(String message, String fileName) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "200");
        obj.put("message", message);
        obj.put("filename", fileName);
        return obj.toJSONString();
    }

    /**
     * 后台框架文件上传错误JSON
     *
     * @param message
     * @return
     */
    public static String toUploadError(String message) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "300");
        obj.put("message", message);
        return obj.toJSONString();
    }

    /**
     * 后台框架请求错误JSON
     *
     * @param message
     * @return
     */
    public static String toError(String message) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "300");
        obj.put("message", message);
        obj.put("tabid", "");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架请求错误JSON
     *
     * @param code
     * @return
     */
    public static String toError(int code) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "300");
        obj.put("message", ErrorCode.getDesc(code));
        obj.put("tabid", "");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架回话超时JSON
     *
     * @return
     */
    public static String toTimeout() {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "301");
        obj.put("message", "会话超时");
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }

    /**
     * 后台框架回话超时JSON
     *
     * @param info
     * @return
     */
    public static String toTimeout(String info) {
        JSONObject obj = new JSONObject();
        obj.put("statusCode", "301");
        obj.put("message", info);
        obj.put("closeCurrent", false);
        obj.put("forward", "");
        obj.put("forwardConfirm", "");
        return obj.toJSONString();
    }


    /**
     * 通用信息提示JSON
     *
     * @param success
     * @param msg
     * @return
     */
    public static Map toMap(boolean success, String msg) {
        Map obj = Maps.newHashMap();
        obj.put("success", success);
        obj.put("message", msg);
        return obj;
    }

    /**
     * API接口请求成功返回JSON
     *
     * @param msg
     * @param list
     * @return
     */
    public static String toSuccessResultJson(String msg, List list) {
        ApiResult result = new ApiResult<List>();
        result.setStatusCode(DEFAULT_SUCCESS_CODE);
        result.setMsg(msg);
        result.setData(list);
        return JSON.toJSONString(result);
    }

    /**
     * API接口请求成功返回JSON
     *
     * @param code
     * @return
     */
    public static String toSuccessResultJson(int code) {
        ApiResult result = new ApiResult<List>();
        result.setStatusCode(DEFAULT_SUCCESS_CODE);
        result.setMsg(ErrorCode.getDesc(code));
        result.setData(null);
        return JSON.toJSONString(result);
    }

    /**
     * API接口请求成功返回JSON
     *
     * @param msg
     * @param object
     * @return
     */
    public static String toSuccessResultJson(String msg, Object object) {
        ApiResult result = new ApiResult();
        result.setStatusCode(DEFAULT_SUCCESS_CODE);
        result.setMsg(msg);
        if (!CmsUtil.isNullOrEmpty(object)) {
            result.setData(object);
        }
        return JSON.toJSONString(result);
    }

    /**
     * API接口请求错误返回JSON
     *
     * @param msg
     * @return
     */
    public static String toErrorResultJson(String msg) {
        ApiResult result = new ApiResult();
        result.setStatusCode(DEFAULT_ERROR_CODE);
        result.setMsg(msg);
        return JSON.toJSONString(result);
    }

    /**
     * API接口请求错误返回JSON
     *
     * @param code
     * @return
     */
    public static String toErrorResultJson(int code) {
        ApiResult result = new ApiResult();
        result.setStatusCode(DEFAULT_ERROR_CODE);
        result.setMsg(ErrorCode.getDesc(code));
        return JSON.toJSONString(result);
    }

    public static String objectTojson(Object object) {
        return JSON.toJSONString(object, SerializerFeature.WriteDateUseDateFormat);
    }

    /**
     * 创建一个新的JSONObject
     *
     * @return
     */
    public static JSONObject newJsonObject() {
        return new JSONObject();
    }
}
